require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../lib/flexible')
require('../font/iconfont.css')
require('../css/register.less')
    //验证码插件
const CaptchaMini = require("captcha-mini")
    //引入axios
const axios = require('axios')



document.addEventListener('DOMContentLoaded', function() {
    const phone = document.querySelector('.phone')
    const authCode = document.querySelector('.authCode')
    const password = document.querySelector('.password')
    const againpw = document.querySelector('.againPW')
    const btnRegister = document.querySelector('.btnRegister')
    const errMsg = document.querySelector('#errMsg') //错误提示
    const b = document.querySelector('b')


    /**
     * 0.定义数据
     */
    let code = ''
        /*传值,参数配置值，选择性配置*/
    let captcha = new CaptchaMini({
        lineWidth: 1, //线条宽度
        lineNum: 6, //线条数量
        dotR: 2, //点的半径
        dotNum: 25, //点的数量
        preGroundColor: [10, 80], //前景色区间
        backGroundColor: [150, 250], //背景色区间
        fontSize: 40, //字体大小
        fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'], //字体类型
        fontStyle: 'fill', //字体绘制方法，有fill和stroke
        content: 'fdgdfgghabcdefghijklmnopqrstuvwcanvas', //验证码内容
        length: 6 //验证码长度
    });
    captcha.draw(document.querySelector('#captcha'), function(r) {
        //r:验证码
        console.log(r);
        code = r;
    });

    btnRegister.addEventListener('click', function() {
        // 这里的inputvalue变量值要写在click事件里面,(点击后才获取值)
        const phoneVal = phone.value
        const passwordVal = password.value
        const againpwVal = againpw.value
        if (!/^1[3-9][0-9]{9}$/.test(phoneVal)) {
            errMsg.style.display = 'block'
            errMsg.textContent = '手机号错误'
            return
        } else if (authCode.value != code) {
            errMsg.style.display = 'block'
            errMsg.textContent = '验证码错误'
            return
        } else if (!/^[a-zA-Z]\w{5,11}$/.test(passwordVal)) {
            errMsg.style.display = 'block'
            errMsg.textContent = '密码错误'
            return
        } else if (againpwVal !== passwordVal) {
            errMsg.style.display = 'block'
            errMsg.textContent = '两次密码不一致密码'
        } else {
            errMsg.textContent = ''
            axios.post('http://139.9.177.51:8099/users/add', {
                "account": phoneVal,
                "password": passwordVal
            }).then(function(res) {
                if (res.data.status === 0) {
                    location.href = "./login.html"
                    console.log(res);
                } else {
                    console.log('失败');

                }

            })
        }



    })
    phone.addEventListener('input', function() {
        if (phone.value !== '') {
            b.style.display = 'none'
        } else {
            b.style.display = 'block'
        }
    })

})